<?php


namespace app\services\order;


use app\models\coupon\CouponModel;
use app\models\coupon\UserCouponModel;

class CouponService
{
    /**
     * 不可用原因
     * @param $info
     * @param $goodsId
     * @param $amount
     * @return string
     */
    public static function getReason($info, $goodsId, $amount)
    {
        if ($info['use_start_time'] > time()) {
            return '未到使用时间';
        }
        if ($info['use_end_time'] < time()) {
            UserCouponModel::where('coupon_id',$info['coupon_id'])->update(['status' => 1]);
            return '已过期';
        }
        if ($amount && $info['less_amount'] > $amount) {
            return '订单不满' . $info['less_amount'] . '元';
        }
        $type = $info['use_type'];
        $typeIdArr = explode(',', $info['type_id']);
        if ($goodsId && $type == 1 && !in_array($goodsId, $typeIdArr)) {
            return '该服务不可用';
        }
        if ($goodsId && $type == 2 && in_array($goodsId, $typeIdArr)) {
            return '该服务不可用';
        }
        return '';
    }
}